﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using com.guojin.mj.net.message.game;
using com.guojin.mj.net.message.login;
using com.guojin.mj.net.message.club;
using com.guojin.mj.net.message;
using DG.Tweening;
using System;

public enum ButtonName
{
   startBtn,
   vieZhuangBtn,
   noNiuBtn,
   inviteBtn,  
}

public class GameRoomPage : EventManager
{
    private static GameRoomPage instance;
    public static GameRoomPage GetInstance()
    {
        return instance;
    }
    void Awake()
    {
        instance = this;
    }

    public override void InformationSetting()
    {
        GameObject cav = GameObject.Find("Canvas").gameObject;
        aud = cav.transform.GetComponents<AudioSource>()[0];
        aud2 = cav.transform.GetComponents<AudioSource>()[1];

        //设置 房间信息 牌型设置 开始 邀请 自动算牛 自动下注 实时战绩
        AddListenerToButton(SetBtn, MoreSetBtnClick);
        AddListenerToButton(RoomInfoBtn, RoomInfoBtnClick);
        AddListenerToButton(CardTypeBtn, CardTypeBtnClick);
        AddListenerToButton(startBtn, StartBtnClick);
        AddListenerToButton(inviteBtn, InviteBtnClick);
        AddListenerToButton(vieForZhuang, VieforZhuangBtnClick);
        AddListenerToButton(noNiubtn, NoNiuBtnClick);
        AddListenerToButton(autoNiuBtn, AutoNiuBtnClick);
        AddListenerToButton(autoBetBtn, AutoBetBtnClick);
        AddListenerToButton(currentWarRecordBtn, CurrentWarRecordBtnClick);
        AddListenerToButton(seatBtn0, SeatBtn0Click);
        AddListenerToButton(seatBtn1, SeatBtn1Click);
        AddListenerToButton(seatBtn2, SeatBtn2Click);
        AddListenerToButton(seatBtn3, SeatBtn3Click);
        AddListenerToButton(seatbtn4, SeatBtn4Click);
        AddListenerToButton(seatBtn5, SeatBtn5Click);
        GameRoomModel.GetInstance().RoomIDChangeEvent += SetRoomID;
        GameRoomModel.GetInstance().RoomNameChangeEvent += SetRoomName;
        GameRoomModel.GetInstance().BotBetChangeEvent += SetBotBet;
    }

    // 0 筹码移动   1 下注（少） 2 下注（多） 3 发牌   4 亮牌  5准备  6 slider点击 7发牌桌面 8 未知
    public  AudioClip[] clips;
    private  AudioSource aud;
    private  AudioSource aud2;        //播放胜利和失败音效

    public Button SetBtn;
    public Button RoomInfoBtn;
    public Button CardTypeBtn;
    public Button startBtn;
    public Button inviteBtn;
    public Button voiceBtn;
    public Button vieForZhuang;
    public Button noNiubtn;
    public Button chatBtn;
    public Button autoNiuBtn;
    public Button autoBetBtn;
    public Button currentWarRecordBtn;

    public GameObject[] loadPointGroup;                        //头像加载位置
    public GameObject MoreSetPage;

    #region //(roomId，botBet , roomID,Model状态改变事件监听)
    public Text roomNameTex;
    public Text BotBetTex;
    public Text roomIDTex;
    private void SetRoomName(string roomName)
    {
        if (GameObject.Find("GameRoomPage(Clone)") && roomNameTex != null)
            roomNameTex.text = string.Format("【{0}】", roomName);
        else
            Debug.Log("设置roomName没有找到GameRoomPage(Clone)面板！");
    }

    private void SetBotBet(int botBet)
    {
        if (GameObject.Find("GameRoomPage(Clone)") && BotBetTex != null)
            BotBetTex.text = string.Format("底分：{0}", botBet.ToString());
        else
            Debug.Log("设置botBet没有找到GameRoomPage(Clone)面板！");
    }

    private void SetRoomID(string roomID)
    {
        if (GameObject.Find("GameRoomPage(Clone)") && roomIDTex != null)
        {
            if (GameRoomModel.GetInstance().RoomType == 0)
            {
                SetText(roomIDTex, string.Format("房间号：{0}", roomID));
            }
            else if (GameRoomModel.GetInstance().RoomType == 1)
            {
                Debug.Log(GameRoomModel.GetInstance().ClubOrLianMengName);
                SetText(roomIDTex, GameRoomModel.GetInstance().ClubOrLianMengName);
            }
            else if (GameRoomModel.GetInstance().RoomType == 2)
            {
                Debug.Log(GameRoomModel.GetInstance(). ClubOrLianMengName);
                string[] str = GameRoomModel.GetInstance().ClubOrLianMengName.Split(',');
                SetText(roomIDTex, str[0]);
            }
        }
     
        else
            Debug.Log("重置RoomId没有找到GameRoomPage(Clone)面板！");
    }
    #endregion

    #region    //坐下按钮点击事件
    public Button seatBtn0;
    public Button seatBtn1;
    public Button seatBtn2;
    public Button seatBtn3;
    public Button seatbtn4;
    public Button seatBtn5;

    private void SeatBtn0Click()
    {
        Debug.Log("当前玩家locationIndex" + GameData.GetInstance().locationIndex);
        Debug.Log("查看座位是否包含" + Array.IndexOf(GameController.GetInstance().intArr, 0));
        if (GameData.GetInstance().locationIndex == -1 && !GameRoomModel.GetInstance().playerDic.ContainsKey(Array.IndexOf(GameController.GetInstance().intArr,0)))
        {
            MessageManager.GetInstance().SendMsg1231(1, int.Parse(GameData.GetInstance().userID));
        }
    }
    private void SeatBtn1Click()
    {
        Debug.Log("当前玩家locationIndex" + GameData.GetInstance().locationIndex);
        Debug.Log("查看座位是否包含" + Array.IndexOf(GameController.GetInstance().intArr, 1));
        if (GameData.GetInstance().locationIndex == -1 && !GameRoomModel.GetInstance().playerDic.ContainsKey(Array.IndexOf(GameController.GetInstance().intArr, 1)))
        {
            MessageManager.GetInstance().SendMsg1231(1, int.Parse(GameData.GetInstance().userID));
        }
    }
    private void SeatBtn2Click()
    {
        Debug.Log("当前玩家locationIndex" + GameData.GetInstance().locationIndex);
        Debug.Log("查看座位是否包含" + Array.IndexOf(GameController.GetInstance().intArr, 2));
        if (GameData.GetInstance().locationIndex == -1 && !GameRoomModel.GetInstance().playerDic.ContainsKey(Array.IndexOf(GameController.GetInstance().intArr, 2)))
        {
            MessageManager.GetInstance().SendMsg1231(1, int.Parse(GameData.GetInstance().userID));
        }
    }
    private void SeatBtn3Click()
    {
        Debug.Log("当前玩家locationIndex" + GameData.GetInstance().locationIndex);
        Debug.Log("查看座位是否包含" + Array.IndexOf(GameController.GetInstance().intArr, 3));
        if (GameData.GetInstance().locationIndex == -1 && !GameRoomModel.GetInstance().playerDic.ContainsKey(Array.IndexOf(GameController.GetInstance().intArr, 3)))
        {
            MessageManager.GetInstance().SendMsg1231(1, int.Parse(GameData.GetInstance().userID));
        }
    }
    private void SeatBtn4Click()
    {
        Debug.Log("当前玩家locationIndex" + GameData.GetInstance().locationIndex);
        Debug.Log("查看座位是否包含" + Array.IndexOf(GameController.GetInstance().intArr, 4));
        if (GameData.GetInstance().locationIndex == -1 && !GameRoomModel.GetInstance().playerDic.ContainsKey(Array.IndexOf(GameController.GetInstance().intArr, 4)))
        {
            MessageManager.GetInstance().SendMsg1231(1, int.Parse(GameData.GetInstance().userID));
        }
    }
    private void SeatBtn5Click()
    {
        Debug.Log("当前玩家locationIndex" + GameData.GetInstance().locationIndex);
        Debug.Log("查看座位是否包含" + Array.IndexOf(GameController.GetInstance().intArr, 5));
        if (GameData.GetInstance().locationIndex == -1 && !GameRoomModel.GetInstance().playerDic.ContainsKey(Array.IndexOf(GameController.GetInstance().intArr, 5)))
        {
            MessageManager.GetInstance().SendMsg1231(1, int.Parse(GameData.GetInstance().userID));
        }
    }
    #endregion

    private void MoreSetBtnClick()
    {
        if (MoreSetPage.activeInHierarchy)
        {
            MoreSetPage.SetActive(false);
        }
        else
        {
            MoreSetPage.SetActive(true);
            MoreSetPage.GetComponent<MoreSetPage>().InitBtnState();
        }

        GameData.GetInstance().SetIsClickThisChapterTrue();
    }

    private void RoomInfoBtnClick()                        
    {
        UIManager.ChangeUI(UIManager.PageState.RoomInfoPage, (GameObject obj) =>
        {
            obj.GetComponent<RoomInfoPage>().InformationSetting();
        });

        GameData.GetInstance().SetIsClickThisChapterTrue();
    }

    private void CardTypeBtnClick()
    {
        UIManager.ChangeUI(UIManager.PageState.RoomRulePage, (GameObject obj) =>
        {
            obj.GetComponent<RoomRulePage>().InformationSetting();
        });

        GameData.GetInstance().SetIsClickThisChapterTrue();
    }

    private void StartBtnClick()
    {
        if (isStart)
        {
            SetStartBtn(0);
            SetInviteBtn(0);
            MessageManager.GetInstance().SendMsg121();    
            isStart = false;
        }   
    }

    private void InviteBtnClick()
    {
        LoginAndShare.Controller.OnClickShareInfo(URLConst.ShareUrl);
    }

    private void VieforZhuangBtnClick()
    {
        MessageManager.GetInstance().SendMsg1237(GameData.GetInstance().locationIndex);

        GameData.GetInstance().SetIsClickThisChapterTrue();
    }

    private void NoNiuBtnClick()
    {
        MessageManager.GetInstance().SendMsg1210(GameData.GetInstance().locationIndex, 0,
            GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].HandCardsListToInt());

        GameData.GetInstance().SetIsClickThisChapterTrue();
    }

    public void OpenAutoNiu()
    {
        autoNiuBtn.GetComponent<Image>().sprite = Resources.Load<Sprite>("UI/GameRoom/自动算牛1");
        GameRoomModel.GetInstance().AutoNiu = true;
    }


    private void AutoNiuBtnClick()
    {
        GameRoomModel.GetInstance().AutoNiu = !GameRoomModel.GetInstance().AutoNiu;

        if (GameRoomModel.GetInstance().AutoNiu)
        {
            autoNiuBtn.GetComponent<Image>().sprite = Resources.Load<Sprite>("UI/GameRoom/自动算牛1");
            GameRoomModel.GetInstance().AutoNiu = true;
            GameRoomController.GetInstance().CheckAutoNiuOrPlayerDeal();
        }
        else
        {
            autoNiuBtn.GetComponent<Image>().sprite = Resources.Load<Sprite>("UI/GameRoom/自动算牛0");
            GameRoomModel.GetInstance().AutoNiu = false;
        }

        GameData.GetInstance().SetIsClickThisChapterTrue();
    }

    private void AutoBetBtnClick()
    {
        GameRoomModel.GetInstance().AutoBet = !GameRoomModel.GetInstance().AutoBet;

        if (GameRoomModel.GetInstance().AutoBet)
        {
            autoBetBtn.GetComponent<Image>().sprite = Resources.Load<Sprite>("UI/GameRoom/自动投注1");
            GameRoomModel.GetInstance().AutoBet = true;
            GameRoomController.GetInstance().CheckAutoBetOrPlayerBet();
        }
        else
        {
            autoBetBtn.GetComponent<Image>().sprite = Resources.Load<Sprite>("UI/GameRoom/自动投注0");
            GameRoomModel.GetInstance().AutoBet = false;
        }

        GameData.GetInstance().SetIsClickThisChapterTrue();
    }

    private void CurrentWarRecordBtnClick()
    {
        UIManager.ChangeUI(UIManager.PageState.CurrentWarRecordPage, (GameObject obj) =>
        {
            obj.GetComponent<CurrentWarRecordPage>().InformationSetting();
        });

        GameData.GetInstance().SetIsClickThisChapterTrue();
    }

    //设置btn 的状态和UI显示
    public void SetStartBtn(int btnState)
    {
        GameRoomPage.GetInstance().SetBtnState(ButtonName.startBtn, btnState);
    }

    public void SetVieZhuangBtn(int btnState)
    {
        GameRoomPage.GetInstance().SetBtnState(ButtonName.vieZhuangBtn, btnState);

        vieForZhuang.transform.FindChild("vieNum").GetComponent<Text>().text =
            Math.Ceiling((double)GameRoomModel.GetInstance().MinTakeInScore/250).ToString();
    }

    public void SetNoNiuBtn(int btnState)
    {
        GameRoomPage.GetInstance().SetBtnState(ButtonName.noNiuBtn, btnState);
    }

    public void SetInviteBtn(int btnState)
    {
        GameRoomPage.GetInstance().SetBtnState(ButtonName.inviteBtn, btnState);
    }

    public List<Button> btnList = new List<Button>();
    private void SetBtnState(ButtonName buttonName, int btnState)                  //btnState  0 不激活  1 激活允许点击  2激活不允许点击
    {
        if (!btnList[(int)buttonName].gameObject.activeInHierarchy &&  btnState != 0)
        {
            btnList[(int)buttonName].gameObject.SetActive(true);
        }

        if (btnState == 0)
        {
            btnList[(int)buttonName].GetComponent<Button>().interactable = false;
            btnList[(int)buttonName].gameObject.SetActive(false);
        }
        else if ( btnState == 1)
        {
            btnList[(int)buttonName].GetComponent<Button>().interactable = true;
        }
        else if (btnState == 2)
        {
            btnList[(int)buttonName].GetComponent<Button>().interactable = false;
        }
    }

    public void SetAudAndPlay(AudioClip clip)
    {
        aud.clip = clip;
    }

    public void SetAud2AndPlay(string str)
    {
        int num = GetVideoNum(str);
        if (num != -1)
        {
            aud2.clip = clips[num];
            aud2.Play();
        }
    }

    private int GetVideoNum(string str)
    {
        int num = -1;
        switch (str)
        {
            case "散牌":
                num = 0;
                break;
            case "牛一":
                num = 1;
                break;
            case "牛二":
                num = 2;
                break;
            case "牛三":
                num = 3;
                break;
            case "牛四":
                num = 4;
                break;
            case "牛五":
                num = 5;
                break;
            case "牛六":
                num = 6;
                break;
            case "牛七":
                num = 7;
                break;
            case "牛八":
                num = 8;
                break;
            case "牛九":
                num = 9;
                break;
            case "牛牛":
                num = 10;
                break;
            case "炸弹":
                num = 11;
                break;
            case "五花":
                num = 12;
                break;
            case "五小":
                num = 13;
                break;
        }
        return num;
    }



    bool isStart = true;           //能够点击开始准备按钮
    float startTimer = 0;
    void Update()
    {
        if (!isStart)
        {
            startTimer += Time.deltaTime;
            if (startTimer > 2)
            {
                startTimer = 0;
                isStart = true;
            }
        }
    }
}
